bwController.$inject = ['$scope', '$stateParams', '$http', '$location'];
var io = require('socket.io-client');
const socket = io('http://zha-ita077-w7:3001');
const socket2 = io('ws://zha-ita012-w7:3001');

function bwController($scope, $stateParams, $http, $location) {
    $scope.status = $stateParams.status;
    var vm = this;
    // const MAX_PAGE_SIZE = 7;
    // vm.page_count = 1;

    if ($scope.status == 'black_list')
        vm.tableTitle = 'Merchant Black List'
    else
        vm.tableTitle = 'Merchant White List';
    //get merchant datas
    $http({
        url: "/api/admin/manage/merchants/status/" + $scope.status,
        method: 'GET',
    }).then(function(response) {

        vm.merchants = response.data;
        console.log(vm.merchants)
            // vm.merchants_store = response.data;
            // vm.merchants_temp = vm.merchants_store;
            // if (vm.merchants_store.length <= 7)
            // vm.getMerchantForPage(0, vm.merchants_store.length);
            // else
            // vm.getMerchantForPage = getMerchantForPage(0, 7);
    });



    vm.changeWhite = function(merchant) {
        $http({
            url: "/api/admin/manage/merchants/white",
            method: 'PUT',
            data: {
                'id': merchant._id
            }
        }).then(function(response) {
            if (response.data.nModified == 1) {
                socket.emit('notify', { merchantId: merchant._id, message: "You have being white", code: 2, status: "white_list" });
                socket2.emit('change', 'change');
                console.log("1111111");
                vm.deleteMerchantNode(merchant);
                merchant.mStatus = 'white_list';
            }
        })
    }

    vm.changeBlack = function(merchant) {
        $http({
            url: "/api/admin/manage/merchants/black",
            method: 'PUT',
            data: {
                'id': merchant._id,
            }
        }).then(function(response) {
            if (response.data.ok == 1) {
                socket.emit('notify', { merchantId: merchant._id, message: "You have being black", code: 2, status: "black_list" });
                socket2.emit('change', 'change');
                console.log("2222222");
                vm.deleteMerchantNode(merchant);
                merchant.mStatus = 'black_list';
            }
        })
    }

    vm.deleteMerchantNode = function(merchant) {
        for (var i = 0; i < vm.merchants.length; i++) {
            if (vm.merchants[i]._id == merchant._id) {
                vm.merchants.splice(i, 1);
                // vm.merchants_store.splice((vm.page_count - 1) * MAX_PAGE_SIZE, 1);
            }
        }
    }

    // vm.prev_page = function() {
    //     if (vm.page_count > 1) {
    //         console.log('prev page');
    //         vm.getMerchantForPage((vm.page_count - 2) * 7, (vm.page_count - 1) * 7);
    //         vm.page_count--;
    //     }

    // }

    // vm.next_page = function() {
    //     console.log('next page');
    //     console.log(vm.merchants_store.length)
    //     if (vm.merchants_store.length >= (7 * vm.page_count) - 1) {
    //         vm.getMerchantForPage(vm.page_count * 7, 7);
    //         vm.page_count++;
    //     }
    // }

    vm.getMerchantForPage = function(start, end) {
        for (var i = 0; i < vm.merchants_store.length; i++) {
            if (i < end && i >= start)
                vm.merchants.push(vm.merchants_store[i])
        }
    }
}

module.exports = bwController;